//
//  TestInfoViewController.m
//  Molian
//
//  Created by 胡津 on 16/7/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "TestInfoViewController.h"
#import "TestInfoCell.h"
@interface TestInfoViewController ()<UITableViewDelegate, UITableViewDataSource>
// 视图列表
@property (nonatomic, strong)UITableView *tableView;
// 数据列表
@property (nonatomic, strong)NSArray *data;
@property (nonatomic, strong)NSArray *testdata;
@property (nonatomic, strong)NSArray *test;
@property (nonatomic, strong)NSArray *abc;

@end

@implementation TestInfoViewController
{
    BOOL testBool;
    int timu;
    UIButton *btn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.title = @"介绍";
    
    _data = @[@{@"title":@"口袋恋人是一款怎样的APP？",@"data":@[@"它是一款真人虚拟恋爱应用",@"它能造福天下单身汪，去温暖情感孤独的人",@"它让用户通过付费方式获得更好的聊天机会"]},@{@"title":@"什么是专属恋人？",@"data":@[@"专属恋人是社会和时间共享经济模式下的一份兼职，主要工作是假装情侣，陪伴孤独汪聊天，当然也非常有可能遇到真的合适的人"]},@{@"title":@"为什么要成为专属恋人？",@"data":@[@"可以出现在主页",@"可以通过聊天陪伴获得收益",@"可以享受更多APP内特权"]},@{@"title":@"成为专属恋人需要什么条件？",@"data":@[@"有较多闲暇时间",@"热情主动，喜欢聊天交友",@"情商高，会聊天",@"颜值声音棒棒哒",@"有其他加分项目比如唱歌"]},@{@"title":@"专属恋人收益怎么算？",@"data":@[@"对方评价5星获得100%收益",@"对方评价4星获得80%收益",@"对方评价1星至3星无收益"]},@{@"title":@"专属恋人严厉禁止哪些？",@"data":@[@"个人资料照片不能过于暴露，不能出现违法字眼",@"不能私下从事色情裸聊活动",@"杜绝微商，个人资料不可出现微信号QQ号等其他联系方式"]},@{@"title":@"专属恋人如何保护自己？",@"data":@[@"坚决不给联系方式，除非遇上真正喜欢的人",@"坚决不透露住址，防止遇到坏蜀黍"]}];

    
    _abc = @[@"a",@"b",@"c",@"d",@"e",@"f"];
    _testdata = @[@{@"title":@"口袋恋人是一款怎样的应用?",@"data":@[@"走心",@"走肾"],@"answer":@0},@{@"title":@"个人资料可以放暴露照片吗",@"data":@[@"可以",@"不可以"],@"answer":@1},@{@"title":@"获得5星评价的收益是多少?",@"data":@[@"全额收益!",@"80%"],@"answer":@0},@{@"title":@"虚拟恋人的终极目标是什么?",@"data":@[@"温暖别人,谈到世界充满爱",@"赚很多钱"],@"answer":@0},@{@"title":@"如果正好有事要怎么办?",@"data":@[@"和对方解释",@"直接不理对方"],@"answer":@0},@{@"title":@"早上醒来跟对方说什么?",@"data":@[@"早安啦",@"没什么好说的"],@"answer":@0},@{@"title":@"对方一直要微信号怎么办",@"data":@[@"委婉拒绝",@"给就给吧,万一是土豪呢"],@"answer":@0},@{@"title":@"对方发过分文字、提无理要求怎么办?",@"data":@[@"转移话题,再过分就举报",@"不理睬对方"],@"answer":@0},@{@"title":@"个人资料可以留联系方式吗?",@"data":@[@"可以",@"不行,万一你卖面膜呢"],@"answer":@1},@{@"title":@"聊天的小妹妹哭着喊着要嫁给你怎么办?",@"data":@[@"正确指导,不要迷上哥",@"告诉地址,请吃麻辣烫"],@"answer":@0},@{@"title":@"对方只会说哦 呵呵怎么办",@"data":@[@"我也只能哦,呵呵",@"发掘他的兴趣,引导他多说"],@"answer":@1},@{@"title":@"对方情绪失落怎么办?",@"data":@[@"鼓励他让他找回自信",@"不要把负能量传染给我啊"],@"answer":@0},@{@"title":@"倒霉地遇到谩骂的用户,怎么办?",@"data":@[@"我妈都没这么骂过我,反骂回去!",@"表现成熟，引导病人平复心情"],@"answer":@1},@{@"title":@"下面哪项会让人快乐?",@"data":@[@"和对方聊小时候的开心事",@"工作上的烦心事"],@"answer":@0},@{@"title":@"下面哪项是正确?",@"data":@[@"及时回复别人",@"遇到不喜欢就不理~"],@"answer":@0}];

    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = YES;
    self.tableView.separatorColor = [UIColor clearColor];//分隔条颜色
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);//偏移量
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);//滚动条偏移量
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
    //    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    self.tableView.tableFooterView = [[UIView alloc] init];//不显示空的cell
    [self.view addSubview:self.tableView];
    testBool = 1;

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //登陆按钮
        BFPaperButton *test = [[BFPaperButton alloc]initWithFrame:CGRectMake(20, self.tableView.contentSize.height, DeviceWidth-40, 45 )raised:NO];
        [test setTitle:@"开始测试" forState:UIControlStateNormal];
        [test setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        test.layer.masksToBounds = YES;//开启圆角
        [test setBackgroundImage:[UIImage imageWithColor:APPCOLOR(1)] forState:UIControlStateNormal];
        [test.layer setCornerRadius:CGRectGetHeight([test bounds]) / 2];
        test.titleLabel.font = [UIFont fontWithName:FontName size:14];
        [test addTarget:self action:@selector(addTest:) forControlEvents:UIControlEventTouchUpInside];
        [self.tableView addSubview:test];
    });
    btn = [[UIButton alloc]initWithFrame:self.view.frame];
    [btn setImage:[UIImage imageNamed:@"test_cancel_icon"] forState:UIControlStateNormal];
    [btn setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.7]];
    btn.alpha = 0;
    [self.view addSubview:btn];
    
}

-(void)addTest:(UIButton*)sender
{
    testBool = 0;
    [self.tableView reloadData];
    [Utils Hidden:sender Bool:NO];
    [sender removeFromSuperview];

    UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%d/15",timu+1] style:UIBarButtonItemStylePlain target:nil action:nil];
    rightItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *CellIdentifier = @"TestInfoCell";
    TestInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        cell = [[TestInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    

    cell.neirong.text = testBool ? [_data[indexPath.section] objectForKey:@"data"][indexPath.row]:[_testdata[timu] objectForKey:@"data"][indexPath.row];
    cell.ico.text = _abc[indexPath.row];
    if (testBool == 0) {
        cell.ico.textColor = APPCOLOR(1);
        cell.ico.backgroundColor = [UIColor clearColor];
        [cell.ico.layer setBorderColor:APPCOLOR(1) .CGColor];
        [cell.ico.layer setBorderWidth:1];
    }

    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(void)ok
{
    NSUserDefaults *userD =[NSUserDefaults standardUserDefaults];
    MLUser *user = [MLUser currentUser];
    [userD setObject:@"YES" forKey:[NSString stringWithFormat:@"%@TestInfoViewController",user.objectId]];
    [userD synchronize];
    
    [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count-3] animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ( testBool != 0) {
        return;
    }
    
    if ([[_testdata[timu] objectForKey:@"answer"] integerValue] == indexPath.row) {
        NSLog(@"正确");
        timu ++;
        if (_testdata.count == timu) {
            UIView *view = [[UIView alloc]initWithFrame:self.view.frame];
            view.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:view];
            
            CGFloat imagesize = DeviceWidth-50;

            
            CGFloat userimagesize = IS_IPHONE_6_PLUS ? 100:(imagesize-130)/2;

            UIImageView *user_image = [[UIImageView alloc]initWithFrame:CGRectMake(((DeviceWidth-userimagesize)/2), ((DeviceHeight-userimagesize)/2), userimagesize, userimagesize)];
            [user_image sd_setImageWithURL:[NSURL URLWithString:[MLUser currentUser].avatar.url]];
            if (![MLUser currentUser].subAvatar || [[MLUser currentUser].subAvatar isEqualToString:@""]) {
                [user_image sd_setImageWithURL:[NSURL URLWithString:[MLUser currentUser].avatar.url] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
            }else{
                [user_image sd_setImageWithURL:[NSURL URLWithString:[MLUser currentUser].subAvatar] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
            }
            [view addSubview:user_image];
            
            
            UIImageView *test_image = [[UIImageView alloc]initWithFrame:CGRectMake((DeviceWidth-imagesize)/2, (DeviceHeight-imagesize)/2, imagesize, imagesize)];
            test_image.image = [UIImage imageNamed:@"test_pass_image"];
            [view addSubview:test_image];
            
            UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(ok)];
            rightItem.enabled = YES;
            self.navigationItem.rightBarButtonItem = rightItem;

            UIBarButtonItem *Item  = [[UIBarButtonItem alloc]initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:nil];
            Item.enabled = NO;
            self.navigationItem.leftBarButtonItem = Item;

            
        }else{
            UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%d/15",timu+1] style:UIBarButtonItemStylePlain target:nil action:nil];
            rightItem.enabled = NO;
            self.navigationItem.rightBarButtonItem = rightItem;
            [self.tableView reloadData];
        }
    }else{
        NSLog(@"错误");
        btn.alpha = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [Utils Hidden:btn Bool:NO];
        });
        
    }
    

}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return testBool ? [Utils LabeSizeLabelheight:[_data[indexPath.section] objectForKey:@"data"][indexPath.row] Font:[UIFont fontWithName:FontName size:16]].height+15:[Utils LabeSizeLabelheight:[_testdata[timu] objectForKey:@"data"][indexPath.row] Font:[UIFont fontWithName:FontName size:16]].height+15;
}


//section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return testBool ? [_data count]:1;
}

//cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return testBool ? [[[_data objectAtIndex:section] objectForKey:@"data"]count]:[[[_testdata objectAtIndex:timu] objectForKey:@"data"]count];//Cell个数
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //头cell
    UIView *headView = [UIView new];
    
    UILabel *ico = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 25, 25)];
    ico.layer.masksToBounds = YES;//开启圆角
    ico.textColor =[UIColor whiteColor];
    ico.textAlignment =  NSTextAlignmentCenter;
    ico.backgroundColor = APPCOLOR(1);
    [ico.layer setCornerRadius:25/2];
    [ico setContentMode:UIViewContentModeScaleAspectFill];
    ico.userInteractionEnabled = YES;
    ico.text = testBool ? [NSString stringWithFormat:@"%ld",(long)section+1]:[NSString stringWithFormat:@"%d",timu+1];
    [headView addSubview:ico];
    
    
    //名字
    UILabel *neirong = [[UILabel alloc]initWithFrame:CGRectMake(65, 2.5, DeviceWidth-120, testBool ? [Utils LabeSizeLabelheight:[_data[section] objectForKey:@"title"] Font:[UIFont fontWithName:FontName size:16]].height : [Utils LabeSizeLabelheight:[_testdata[timu] objectForKey:@"title"] Font:[UIFont fontWithName:FontName size:16]].height)];
    neirong.textColor = [UIColor blackColor];
    neirong.font = [UIFont fontWithName:FontName size:16];
    neirong.numberOfLines = 0;
    neirong.text = testBool ? [_data[section]objectForKey:@"title"]:[_testdata[timu]objectForKey:@"title"];
    [headView addSubview:neirong];
    return headView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return testBool ? [Utils LabeSizeLabelheight:[_data[section] objectForKey:@"title"] Font:[UIFont fontWithName:FontName size:16]].height+20 :  [Utils LabeSizeLabelheight:[_testdata[section] objectForKey:@"title"] Font:[UIFont fontWithName:FontName size:16]].height+20;//每个cell的头部间距
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 40;//每个cell的底部间距
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
